if (window.plus) {
    ready();
} else {
    document.addEventListener('plusready', ready, false);
}

$(function(){
  if(AGENT_CLIENT.WEB){
    ready();
  } 
})


function ready(){
  var html = [], 
  thisPage = {}, 
  user = {}, 
  uploadControl = {};

  var showPublishPage = function () {
      this.init();
  };

  showPublishPage.prototype = {
    init: function () {
        this.initData();
        this.bindEvent();
    },
    initData: function () {
      user = getLocalStorage(STORAGE_MANAGER.user);
      if( !user ){
        redirect("../account/login.html");
      }
      this.paintUploadHtml();

      
    },
    paintUploadHtml:function(){
      html.push('<span name="selectUploadType" class="tianjiaan" style="position: relative;">');
      if(AGENT_CLIENT.WEB){
        html.push('<input name="addImgFile" class="a_upload" type="file" capture="camera" accept="image/png,image/gif,image/jpg,image/jpeg" style="height: 70px;"/>');
      }
      html.push('<img src="../../images/tjantu.png" />');
      html.push('</span>');      
      $("#showAlbums").html(html.join(''))
    },
    bindEvent: function () {
		//回退
		$("#back").on("click", function () {
			back();
		});
      

      //上传绑定
      uploadControl = new uploadPage(1);      
      uploadControl.bindUploadShows();
      
      //提交
      this.commitShow();
    },
    commitShow:function(){
      //提交星粉秀
      $("#commitFans").on("click", function(){
        if( "" == $("#memo").val() ){
          $.toast("请填写评论");
          return false;
        }
        if( 0 == $("img[name='addImage']").length ){
          $.toast("请选择图片");
          return false;
        }
        var nemoContent = $("#memo").val();
        console.log(nemoContent);
        var requestData = {
          memo: removeEmoji( nemoContent ),
          member_id:user.id,
          id:user.id,
          content:[],
        };
        console.log(JSON.stringify(requestData));
        $.each($("img[name='addImage']"), function(i, img) {
          var src = $(this).attr('src');
          var index = src.indexOf(',');
          var newStrings = src.substr(index+1);
          requestData.content.push(newStrings);
        });
        
        
        
        $.ajax({
          type: "POST",
          url: CONFIG_MANAGER.RequstUrl + "v1/fans/release/" + user.id,
          async: true,
          contentType: "application/json",
          data: JSON.stringify( requestData ),
          headers:{ token:user.token },
          beforeSend:function(){ $.showWaiting(); }
        }).done(function(data){
          redirect("show.html");
        }).fail(function(data){
          $.toast(data);
        }).always(function(data){ 
          $.closeWaiting();
        });
        
        
      });      
    }
  };

  thisPage = new showPublishPage();
}